package com.v5ba.server.event;

import com.v5ba.server.RocketmqEvent;
import com.v5ba.server.dao.SysLogRepository;
import com.v5ba.server.entity.SysLog;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author yiren
 * @date 2018-08-22
 */
@Component
public class ConsumerEvent {
    @Autowired
    private SysLogRepository sysLogRepository;

    @EventListener(condition = "#event.topic=='zhanghui_topic' && #event.tag=='tagsA-test'")
    public void msgListen(RocketmqEvent event) {
        DefaultMQPushConsumer consumer = event.getConsumer();
        try {
            System.out.println("ConsumerEvent监听到一个消息达到：" + event.getMsg());
            //TODO 进行业务处理
        } catch (Exception e) {
            if(event.getMessageExt().getReconsumeTimes()<=3){//重复消费3次
                try {
                    consumer.sendMessageBack(event.getMessageExt(), 2);
                } catch (Exception e1) {
                    //TODO 消息消费失败，进行日志记录
                }
            } else {
                //TODO 消息消费失败，进行日志记录

            }
        }
    }

    @EventListener(condition = "#event.topic=='zhanghui_topic_order' && #event.tag=='tagsA-log-tran'")
    public void msgListenTran(RocketmqEvent event) {
        DefaultMQPushConsumer consumer = event.getConsumer();
        try {
            System.out.println("ConsumerEvent监听到一个消息达到：" + event.getMsg());
            SysLog sysLog = new SysLog();
            sysLog.setType("系统日志");
            sysLog.setContent("这个是mq 发送的第统日志");
            sysLogRepository.save(sysLog);
            //TODO 进行业务处理
        } catch (Exception e) {
            if(event.getMessageExt().getReconsumeTimes()<=3){//重复消费3次
                try {
                    consumer.sendMessageBack(event.getMessageExt(), 2);
                } catch (Exception e1) {
                    //TODO 消息消费失败，进行日志记录
                }
            } else {
                //TODO 消息消费失败，进行日志记录

            }
        }
    }



}
